using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AD.Pms.Core;
using AD.Pms.Core.ClientApi;
using AD.Pms.Core.EnumHelper;
using AD.Pms.DataBase.Models.ZC_Config;
using AD.Pms.DataBase.Models.ZC_Log;
using AD.Pms.DataBase.Models.ZC_Member;
using AD.Pms.DataBase.Models.ZC_Order;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace AD.Pms.Api.Controllers.V1
{
    [Route("api/v1/[controller]/[action]")]
    [ApiController]
    public class OrderController
    {
        private readonly ZC_OrderContext _zcOrderContext;

        public OrderController(ZC_OrderContext zcOrderContext)
        {
            _zcOrderContext = zcOrderContext;
        }

        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="starttime">开始时间</param>
        /// <param name="endtime">结束时间</param>
        /// <param name="orderstatus">订单类型</param>
        /// <param name="isexport">是否导出</param>
        /// <returns></returns>
        public async Task<ActionResult> OrderList(DateTime? starttime, DateTime? endtime, int? orderstatus,
            bool? isexport)
        {
            var res = new ApiResult<List<TblOrder>>();
            starttime = starttime ?? DateTime.Now.AddDays(-7).Date;
            endtime = endtime ?? DateTime.Now.Date;
            var result = _zcOrderContext.TblOrder.Where(x => x.CreateTime >= starttime && x.CreateTime <= endtime);
            if (orderstatus.HasValue)
            {
                result = result.Where(x => x.OrderStatus.HasValue && x.OrderStatus.Value == orderstatus.Value);
            }

            var data = await result.ToListAsync();
            if (isexport.HasValue && isexport.Value)
            {
                NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                ISheet sheet1 = book.CreateSheet("Sheet1");
                IRow row1 = sheet1.CreateRow(0);
                var names = typeof(TblOrder).GetProperties().Select(x => x.Name);
                int index = 0;
                foreach (var name in names)
                {
                    row1.CreateCell(index).SetCellValue(name);
                    index++;
                }

                for (int i = 0; i < data.Count; i++)
                {
                    IRow rowtemp = sheet1.CreateRow(i + 1);
                    index = 0;
                    foreach (var prop in typeof(TblOrder).GetProperties())
                    {
                        rowtemp.CreateCell(index).SetCellValue(prop.GetValue(data[i]).ToString());
                    }
                }

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                book.Write(ms);
                ms.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(ms, "application/vnd.ms-excel");
            }

            res.data = data;
            return new JsonResult(res);
        }
    }
}